<template>
  <div class="container">
    <div class="log-box">
      <div class="log-title">
        <div class="icon">
          <img src="@/assets/img/login-icon.png" alt />
        </div>
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登录</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="tel">
          <el-row>
            <el-col>
              <el-input prefix-icon="el-icon-user-solid" v-model="form.tel" placeholder="请输入手机号"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="password">
          <el-row>
            <el-col>
              <el-input
                prefix-icon="el-icon-user-solid"
                v-model="form.password"
                show-password
                placeholder="请输入密码"
              ></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input prefix-icon="el-icon-user-solid" v-model="form.code" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="8">
              <div class="code">
                <img :src="imgcode" alt @click="changecode" />
              </div>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item prop="agreement">
          <el-row>
            <el-col>
              <div class="agree">
                <el-checkbox v-model="form.agreement" style="align-items: center;margin-right:10px"></el-checkbox>
                <span>我已阅读并同意</span>
                <el-link type="primary" style="align-items: center;">用户协议</el-link>
                <span>和</span>
                <el-link type="primary" style="align-items: center;">隐私条款</el-link>
              </div>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="btn" @click="onSubmit('form')">登录</el-button>
          <!-- <el-button>取消</el-button> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="cancelEvent">注册</el-button>
          <!-- <el-button>取消</el-button> -->
        </el-form-item>
      </el-form>
      <register ref="register"></register>
    </div>
    <div class="right-pic">
      <img src="@/assets/img/login-bigpic.png" alt />
    </div>
  </div>
</template>

<script>
import register from "@/views/register.vue";
import getLoginApi from "@/api/login.js";
import { savelocal, getlocal } from "@/utils/localstorage.js";
export default {
  components: {
    register,
  },
  data() {
    return {
      dialogVisible: false,
      baseUrl: process.env.VUE_APP_URL,
      imgcode: process.env.VUE_APP_URL + "/captcha?type=login",
      form: {
        tel: "",
        password: "",
        code: "",
        agreement: false,
      },
      rules: {
        tel: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 4, max: 4, message: "长度为4个字符", trigger: "blur" },
        ],
        agreement: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback();
              } else {
                callback(new Error("请输入验证码"));
              }
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    // console.log("获取本地token:" + getlocal("token"));
    if (getlocal("token")) {
      this.$router.push("/layout");
    }
  },
  methods: {
    //全局表单验证
    onSubmit(form) {
      // console.log("submit!");
      this.$refs[form].validate((result) => {
        if (result) {
          getLoginApi({
            phone: this.form.tel,
            password: this.form.password,
            code: this.form.code,
          }).then((res) => {
            //成功回调
            console.log(res);
            savelocal("token", res.data.data.token);
            this.$router.push("/layout");
          });
        } else {
          this.$message.error("失败");
        }
      });
    },
    cancelEvent() {
      this.$refs.register.isshow = true;
    },
    changecode() {
      // console.log("请求新的验证码");
      this.imgcode =
        process.env.VUE_APP_URL + "/captcha?type=login&idsdfs=" + Math.random();
    },
  },
};
</script>

<style lang='less'>
html,
body {
  .container {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    background: linear-gradient(#1493fa 28%, #01c6fa 96%);
    display: flex;
    justify-content: space-around;
    align-items: center;
    .el-dialog__header {
      padding: 0;
      width: 100%;
      height: 60px;
      line-height: 60px;
      background: linear-gradient(to right, #1493fa 28%, #01c6fa 96%);
    }
    .log-box {
      width: 478px;
      height: 550px;
      background: #f5f5f5;
      padding: 42px;
      margin-left: 73px;
      margin-top: 119px;
      background-color: #fff;
      align-items: center;
      .log-title {
        display: flex;
        margin-top: 31px;
        margin-bottom: 31px;
        .icon {
          width: 27px;
          height: 29px;
        }
        .t1 {
          margin-right: 10px;
          height: 24px;
          font-size: 24px;
          font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
          font-weight: 400;
          text-align: left;
          color: #0c0c0c;
        }
        .line {
          width: 2px;
          height: 34px;
          background-color: gray;
        }
        .t2 {
          height: 24px;
          font-size: 24px;
          font-family: PingFangSC Regular, PingFangSC Regular-Regular;
          font-weight: 400;
          text-align: left;
          color: #0c0c0c;
          margin-left: 10px;
          //   padding-left: 10px;
          //   border-left: 2px solid gray;
        }
      }
      .code {
        height: 40px;
      }
      .code img {
        width: 100%;
        height: 100%;
      }
      .agree {
        display: flex;
      }
      .btn {
        margin-top: 20px;
        width: 100%;
      }
    }
  }
}
</style>